home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / render2pixmap / util.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  187 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*
  23.  * 1993 Simon Hui -- Silicon Graphics Computer Systems
  24.  */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <GL/gl.h>
  29. #include <GL/glx.h>
  30. #include <GL/glu.h>
  31. #include <X11/keysym.h>
  32. #include "util.h"
  33.  
  34. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  35. {
  36.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  37.     return GL_TRUE;
  38.     }
  39.     return GL_FALSE;
  40. }
  41.  
  42. Window utilCreateWindow( int defW, int defH, int defX, int defY, Window parent,
  43.              int attributes[], char *colorName,
  44.                  int argc, char **argv, char *geometry,
  45.              Display **dpy_ret, XVisualInfo **vi_ret,
  46.              Colormap *cmap_ret, GC *xgc_ret )
  47. {
  48.     Display *dpy;
  49.     Window window;
  50.     XVisualInfo *vi;
  51.     Colormap cmap;
  52.     GC xgc;
  53.     XSetWindowAttributes swa;
  54.     unsigned int width, height;
  55.     XGCValues gcvalues;
  56.     XColor xcolor, exact;
  57.     XSizeHints sh;
  58.     XEvent event;
  59.  
  60.     if (dpy_ret && *dpy_ret) {
  61.     dpy = *dpy_ret;
  62.     } else {
  63.     dpy = XOpenDisplay(0);
  64.     if (!dpy) {
  65.         fprintf(stderr, "Can't connect to display \"%s\"\n",
  66.             getenv("DISPLAY"));
  67.         exit(1);
  68.     }
  69.     }
  70.  
  71.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
  72.     if (!vi) {
  73.     fprintf(stderr, "Cannot find visual on \"%s\"\n",
  74.         getenv("DISPLAY"));
  75.     exit(1);
  76.     }
  77.  
  78.     if (cmap_ret && *cmap_ret) {
  79.     cmap = *cmap_ret;
  80.     } else {
  81.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  82.                    AllocNone);
  83.     }
  84.     if (colorName) {
  85.     if (!XAllocNamedColor(dpy, cmap, colorName, &exact, &xcolor)) {
  86.         fprintf(stderr, "Can't allocate color %s for X\n", colorName);
  87.         exit(1);
  88.     }
  89.     swa.background_pixel = xcolor.pixel;
  90.     } else {
  91.     swa.background_pixel = 0;
  92.     }
  93.     swa.border_pixel = 0;
  94.     swa.colormap = cmap;
  95.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  96.     | KeyReleaseMask;
  97.  
  98.     width = defW;
  99.     height = defH;
  100.     sh.x = defX;
  101.     sh.y = defY;
  102.     if (geometry) {
  103.     int x, y;
  104.     unsigned int w, h;
  105.     int mask = XParseGeometry(geometry, &x, &y, &w, &h);
  106.     if (mask & XValue)    sh.x = x;
  107.     if (mask & YValue)    sh.y = y;
  108.     if (mask & WidthValue)     width = w;
  109.     if (mask & HeightValue)    height = h;
  110.     }
  111.     sh.flags = USPosition | PPosition;
  112.     if (parent == None) parent = RootWindow(dpy, vi->screen); 
  113.     window = XCreateWindow(dpy, parent, sh.x, sh.y,
  114.                width, height,
  115.                0, vi->depth, InputOutput, vi->visual,
  116.                CWBorderPixel|CWColormap|CWEventMask|CWBackPixel,
  117.                &swa);
  118.     XSetStandardProperties(dpy, window, argv[0], argv[0],
  119.                None, argv, argc, &sh);
  120.     XSetWMColormapWindows(dpy, window, &window, 1);
  121.     XMapWindow(dpy, window);
  122.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  123.  
  124.     if (!xgc_ret || !*xgc_ret) {
  125.     gcvalues.background = xcolor.pixel;
  126.     gcvalues.foreground = xcolor.pixel;
  127.     xgc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcvalues);
  128.     }
  129.  
  130.     if (dpy_ret) *dpy_ret = dpy;
  131.     if (vi_ret) *vi_ret = vi;
  132.     if (cmap_ret) *cmap_ret = cmap;
  133.     if (xgc_ret) *xgc_ret = xgc;
  134.  
  135.     return window;
  136. }
  137.  
  138.  
  139. void utilEventLoop( Display *dpy,
  140.             void (*exposeCallback)(void),
  141.             void  (*keyCallback)( KeySym, Bool *exit, Bool *redraw, Bool *tofile),
  142.             void (*displayCallback)(void),
  143.             void (*fileCallback)(void) )
  144. {
  145.     XEvent event;
  146.     Bool redraw = False;
  147.     Bool tofile = False;
  148.     
  149.     for (;;) {
  150.     do {
  151.         XNextEvent(dpy, &event);
  152.         switch (event.type) {
  153.           case Expose:
  154.         if (exposeCallback) {
  155.             (*exposeCallback)();
  156.         } else {
  157.             (*displayCallback)();
  158.         }
  159.         break;
  160.           case KeyPress:
  161.         {
  162.             char buf[100];
  163.             int rv;
  164.             KeySym ks;
  165.             Bool exit = False;
  166.  
  167.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  168.             (*keyCallback)(ks, &exit, &redraw, &tofile);
  169.             if (exit) return;
  170.         }
  171.         break;
  172.         }
  173.     } while (XPending(dpy) != 0);
  174.     if (redraw) {
  175.         (*displayCallback)();
  176.         redraw = False;
  177.     }
  178.     if (tofile) {
  179.         (*displayCallback)();
  180.             (*fileCallback)();
  181.         tofile = False;
  182.     }
  183.     }
  184. }
  185.  
  186.  
  187.